package com.fenghongzhang.day008;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class IMGActivity extends AppCompatActivity {

    private Button down;
    private Button set;
    private ImageView pic;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_i_m_g);


        down = (Button) findViewById(R.id.down);
        set = (Button) findViewById(R.id.set);
        pic = (ImageView) findViewById(R.id.pic);


        set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {//是否挂载
                    File file = Environment.getExternalStorageDirectory();
//                    try {
//                        FileInputStream fileInputStream = new FileInputStream(new File(file, "kawayi.jpg"));
                        File file1 = new File(file, "kawayi.jpg");
                        Bitmap bitmap = BitmapFactory.decodeFile(file1.getAbsolutePath());//从文件目录直接解析
//                        Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream);
                        pic.setImageBitmap(bitmap);
//                    } catch (FileNotFoundException e) {
//                        e.printStackTrace();
//                    }
                }
            }
        });


        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},999);

        down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //下载
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        FileOutputStream out=null;
                        InputStream inputStream=null;//网络连接的输入流
                        HttpURLConnection connection=null;//向SD卡写的输出流
                        try {
                            URL url= new URL("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3432975985,1817100041&fm=26&gp=0.jpg");
                            connection= (HttpURLConnection) url.openConnection();

                            if (connection.getResponseCode()==200){
                                inputStream = connection.getInputStream();

                                if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {//是否挂载
                                    File file = Environment.getExternalStorageDirectory();
                                    out = new FileOutputStream(new File(file,"kawayi.jpg"));
                                    byte[] bytes=new byte[1024];
                                    int len=0;
                                    while((len=inputStream.read(bytes))!=-1){
                                        out.write(bytes,0,len);
                                    }
                                }
                            }
                            out.close();
                            inputStream.close();
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }


                    }
                }).start();
            }
        });

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 999  && grantResults[0] == PackageManager.PERMISSION_GRANTED){

        }else{

        }
    }
}
